function InitGame()
{
	// Create the secret number and initialize numberOfGuesses to 0.
	var secretNumber = Math.floor(Math.random() * 101);
	var numberOfGuesses = 1;

	RunGameLoop(numberOfGuesses, secretNumber);
}

function RunGameLoop(a_numberOfGuesses, a_secretNumber)
{
	// While the user still has guesses left keep playing.
	while (true)
	{
		// Get the current guess.
		var currentGuess = MakeGuess(a_numberOfGuesses);
		
		// Check if the user won.
		if (DidUserWin(currentGuess, a_secretNumber) == true)
		{
			alert("The secret number was " + a_secretNumber + ".\nYou got the correct answer in " + a_numberOfGuesses + " guess(es).");
			break;
		}
		
		// Check if the user wanted to exit.
		if (DidUserWantToExit(currentGuess) == true)
		{
			alert("Game aborted.");
			break;
		}
		
		// If the user didn't win check wether the users guess was to high, to low or couldn't be read. 
		// If it could be read add 1 to the number of guesses.	
		if (currentGuess > a_secretNumber)
		{
			alert(currentGuess + " is not the number. My number is lower.");
			a_numberOfGuesses++;
		}
		else if (currentGuess < a_secretNumber)
		{
			alert(currentGuess + " is not the number. My number is higher.");
			a_numberOfGuesses++;
		}
		else if (isNaN(currentGuess) == true)
		{
			alert("Your guess could not be read please make a new guess");
		}
	}
}

// Checks if the user entered "x" as their guess.
function DidUserWantToExit(a_currentGuess)
{
	if (a_currentGuess == "x")
	{
		return true;
	}
}

// Check if the user won.
function DidUserWin(a_currentGuess, a_secretNumber)
{
	if (a_currentGuess == a_secretNumber)
	{
		return true;
	}
}

// Let the user guess a number, parse the guess as an integer and then return it.
function MakeGuess(a_numberOfGuesses)
{
	if (a_numberOfGuesses == 1)
	{
		return prompt("Guess a number, I'm thinking of a number between 0-100.\n" + 
		              "You can exit by any time by making \"x\" your guess");
	}
	else
	{
		return prompt("Make another guess.");
	}
}